var readForm;

function initReadContent(oneClientData, oneProjectData, selectedSubMenuCode, seq) {
	boardLayout.detachObject(true);
	
	boardLayout.attachHTMLString(
			'<div id="readDiv"></div>');
	
	readContent(seq);
	initReadForm(oneClientData, oneProjectData, selectedSubMenuCode, seq);
}


function initReadForm(oneClientData, oneProjectData, selectedSubMenuCode, seq) {
	var replyCount = 0;
	var formStructure = [
	                     {type : 'settings', position : 'label-left'}
	                     ,{type : 'block', blockOffset : 0
	                    	 ,list : [
	                    	          {type : 'label', label : '작성자', labelWidth : 95, readonly: true}
	                    	          ,{type : 'newcolumn'}
	                    	          ,{type : 'input', name : 'writer', inputWidth : 230, readonly: true}
	                    	          ]
	                     }
	                     ,{type : 'block', blockOffset : 0
	                    	 ,list : [
	                    	          {type : 'label', label : '제목', labelWidth : 95,readonly: true}
	                    	          ,{type : 'newcolumn'}
	                    	          ,{type : 'input', name : 'title', inputWidth : 230,readonly: true}
	                    	          ]
	                     }
	                     ,{type : 'block', blockOffset : 0, name : 'block_fileList'
	                    	 ,list : [
	                    	          {type : 'label', label : '첨부 파일', labelWidth : 95,readonly: true}
	                    	          ,{type : 'newcolumn'}
	                    	          ,{type : 'fieldset', label : '파일 리스트', name : 'fileList', inputWidth : 300}
	                    	          ]
	                     }
	                     ,{type : 'label', label : '내용', readonly: true}
	                     ,{type : 'editor', name : 'input_content', inputWidth : 640, inputHeight : 300
	                    	 ,toolbar:true
	                    	 ,iconsPath: IMAGE_PATH}
	                     ,{type : 'label', label : '<span style="color:red">덧글 '+replyCount+' 개</span>'}
	                     ,{type : 'block', blockOffset : 0, name : 'block_reply'
	                    	 ,list : [
	                    	          {type : 'input', name : 'input_reply', inputWidth : 560, inputHeight : 40}
	                    	          ,{type : 'newcolumn'}
	                    	          ,{type : 'button', name : 'button_reply', value : '덧글', width : 30, height: 50}
	                    	          ]
	                     }
	                     ,{type : 'block', blockOffset : 0, offsetLeft : 510 
	                    	 ,list : [
	                    	          {type : 'button', name : 'button_list', value : '목록', width : 30}
	                    	          ,{type : 'newcolumn'}
	                    	          ,{type : 'button', name : 'button_delete', value : '삭제', width : 30}
	                    	          ]}
	                     ];
	
	readForm = new dhtmlXForm('readDiv',formStructure);
	
	readForm.attachEvent('onButtonClick', function(name){
		if ( name == 'button_list' ) {
			initTable(oneClientData, oneProjectData, selectedSubMenuCode);
		}
		else if ( name == 'button_delete' ) {
			var delConfirm = confirm("해당 글을 삭제 하시겠습니까?"); 
			if ( delConfirm == true ) {
				deleteContent(oneClientData, oneProjectData, selectedSubMenuCode, seq);
			}
		}
		else if ( name == 'button_reply' ) {
			
		}
	});
}

function readContent(seq) {
	var readContent = $.ajax({
		url : '/bwf/board/readContent/'+ seq
		, type : 'GET'
		, dataType : 'json'
	});
	readContent.done(function(boardVo) {
		if ( boardVo != null ) {
			var data = boardVo;
			
			readForm.setItemValue('writer', data['writer']);
			readForm.setItemValue('title', data['title']);
			readForm.setItemValue('input_content', data['content']);

			var fileNameList = new Array();

			if ( data['filename'] == null ) { // No File..
				readForm.removeItem('block_fileList');
			}
			else {
				var fileNames = data['filename'].split(';');

				for ( var i = 0; i < fileNames.length - 1; i++ ) {
					var oneFileLable = {type : 'label', label : '<a href="#" id="' + fileNames[i] + '" onclick="fileDownload(this); return false">' + fileNames[i] + '</a>'};
					fileNameList.push(oneFileLable);
				}

				var itemData = {type : 'block', blockOffset : 0
						,list : fileNameList
				};

				readForm.addItem('fileList', itemData);
			}
			readForm.setReadonly('input_content', true);
		}
	});
	readContent.fail(function(jqXHR, textStatus) {
		alert('Request failed!');
	});
}

function deleteContent(oneClientData, oneProjectData, selectedSubMenuCode, seq) {
	var readContent = $.ajax({
		url : '/bwf/board/deleteContent/'+ seq
		, type : 'GET'
		, dataType : 'json'
	});
	readContent.done(function(boardList) {
		initTable( oneClientData, oneProjectData, selectedSubMenuCode );
	});
	readContent.fail(function(jqXHR, textStatus) {
		alert('Request failed!');
	});
}

function fileDownload(obj) {
	var fileName = obj.id;
	
	if ( fileName != null ) {
		var dotIndex 		= fileName.lastIndexOf('.');
		var pureFileName 	= '';
		var extension 		= '';
		var requestFileName = '';

		console.log('fileName:' + fileName);
		console.log('dotIndex:' + dotIndex);

		if ( dotIndex != -1 ) {
			pureFileName 	= fileName.substr(0, dotIndex);
			extension		= fileName.substr(dotIndex);
		}
		else {
			pureFileName	= fileName;
		}
		
		console.log('pureFileName:' + pureFileName);
		console.log('extension:' + extension);
		
		requestFileName = pureFileName + '.' + extension;
		
		console.log('requestFileName:' + requestFileName);
		
		$.fileDownload('/bwf/board/attachFileDownload/' + requestFileName, {
			successCallback: function (url) {
				console.log('You just got a file download dialog or ribbon for this URL :' + url);
			},
			failCallback: function (html, url) {
				console.log('Your file download just failed for this URL:' + url + '\r\n' +
						'Here was the resulting error HTML: \r\n' + html);
			}
		});
	}
}